package com.wuyi.service.impl.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wuyi.mapper.CategoryMapper;
import com.wuyi.mapper.PostMapper;
import com.wuyi.mapper.ResourceMapper;
import com.wuyi.mapper.UserMapper;
import com.wuyi.pojo.Category;
import com.wuyi.pojo.Post;
import com.wuyi.pojo.Resource;
import com.wuyi.pojo.User;
import com.wuyi.pojo.bo.AdminAddCategoryBO;
import com.wuyi.pojo.bo.AdminCategoryBO;
import com.wuyi.pojo.vo.CategoryVO;
import com.wuyi.service.admin.AdminService;
import com.zc.utils.DateUtil;
import com.zc.utils.JSONResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 曾川
 * @version 1.0.0
 * @ClassName AdminCatServiceImpl.java
 * @Description 管理员分类模块管理
 * @createTime 2021年11月03日 19:53:00
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PostMapper postMapper;
    @Autowired
    private ResourceMapper resourceMapper;


    /**
     * 分类查询
     * @param categoryName
     * @param categoryDesc
     * @return
     */
    @Override
    public List<Category> queryCategory(String categoryName, String categoryDesc) {

        Example example = new Example(Category.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isBlank(categoryName)) {
            criteria.andLike("categoryName", "%" + categoryName + "%");
        }
        if (!StringUtils.isBlank(categoryDesc)) {
            criteria.andLike("categoryDesc", "%" + categoryDesc + "%");
        }

        List<Category> categories = categoryMapper.selectByExample(example);

        return categories;
    }

    @Transactional( propagation = Propagation.REQUIRED)
    @Override
    public JSONResult addCategory(AdminAddCategoryBO categoryBO) {

        Example example = new Example(Category.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("categoryName", categoryBO.getCategoryName());
        Category category1 = categoryMapper.selectOneByExample(example);
        if (null != category1) {
            return JSONResult.errorMsg("已存在该分类");
        }


        Category category = new Category();
        BeanUtils.copyProperties(categoryBO, category);
        category.setCreateTime(new Date());
        category.setUpdateTime(new Date());

        int res = categoryMapper.insert(category);
        if (res != 1) {
            return JSONResult.errorMsg("新增分类失败");
        }
        return JSONResult.ok("新增分类成功");
    }

    /**
     * 分类编辑
     *
     * @param categoryBO
     * @return
     */
    @Transactional( propagation = Propagation.REQUIRED)
    @Override
    public JSONResult editCategory(AdminCategoryBO categoryBO) {

        Category category = new Category();
        BeanUtils.copyProperties(categoryBO, category);
        category.setCategoryId(categoryBO.getCategoryId());
        category.setUpdateTime(new Date());
        int res = categoryMapper.updateByPrimaryKeySelective(category);
        if (res != 1) {
            return JSONResult.errorMsg("修改失败");
        }
        return JSONResult.ok("修改成功");
    }

    /**
     * 删除分类
     *
     * @param categoryId
     * @return
     */
    @Override
    public JSONResult delCategory(Integer categoryId) {

        int res = categoryMapper.deleteByPrimaryKey(categoryId);
        if (res != 1) {
            return JSONResult.errorMsg("删除分类失败");
        }
        return JSONResult.ok("删除分类成功");
    }

    /**
     * 分页查询用户列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Transactional( propagation = Propagation.SUPPORTS)
    @Override
    public PageInfo queryUser(String userName, Integer userRole, Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        List<User> users;
        if (StringUtils.isNotBlank(userName) || null != userRole) {
            if (StringUtils.isNotBlank(userName)) {
                criteria.andLike("userName", "%" + userName + "%");
            }
            if (null != userRole) {
                criteria.andEqualTo("userRole", userRole);
            }
            example.orderBy("createTime").desc();
            users  = userMapper.selectByExample(example);
        } else {
            example.orderBy("createTime").desc();
            users = userMapper.selectAll();
        }

        PageInfo pageInfo = new PageInfo<>(users);
        pageInfo.setList(users);

        return pageInfo;
    }

    @Transactional( propagation = Propagation.REQUIRED)
    @Override
    public JSONResult deleteUser(String userId) {

        int i = userMapper.deleteByPrimaryKey(userId);
        if (i != 1) return JSONResult.errorMsg("删除用户失败");

        return JSONResult.ok("删除用户成功");
    }

    /**
     * 查询帖子
     *
     * @param postTitle
     * @param userName
     * @param postTime
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Transactional( propagation = Propagation.SUPPORTS)
    @Override
    public PageInfo queryPost(String postTitle, String userName,
                              String postTime, Integer pageNum,
                              Integer pageSize) throws ParseException {
        PageHelper.startPage(pageNum,pageSize);
        Example example = new Example(Post.class);
        Example.Criteria criteria = example.createCriteria();
        List<Post> posts = new ArrayList<>();
        //若其中一个有值，则进入条件查询，否则查询全部
        if (StringUtils.isNotBlank(postTitle) || StringUtils.isNotBlank(userName)) {
            if (StringUtils.isNotBlank(postTitle)) {
                criteria.andLike("postTitle","%" + postTitle + "%");
            }
            if (StringUtils.isNotBlank(userName)) {
                criteria.andLike("postUid","%" + userName + "%");
            }
            if (StringUtils.isNotBlank(postTime)) {
                DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
                Date date = format1.parse(postTime.substring(1, postTime.length() - 1));
                criteria.andEqualTo("postTime", date);
            }
            example.orderBy("createTime").desc();

            posts = postMapper.selectByExample(example);
        } else {
            example.orderBy("createTime").desc();
            posts = postMapper.selectAll();
        }


        PageInfo pageInfo = new PageInfo<>(posts);
        pageInfo.setList(posts);

        return pageInfo;
    }
    /**
     * 三个查询资源的模块为一个接口
     * @param resourceName
     * @param userName
     * @param resourceTime
     * @param categoryId
     * @param pageNum
     * @param pageSize
     * @return
     * @throws ParseException
     */
    @Transactional( propagation = Propagation.SUPPORTS)
    @Override
    public PageInfo queryResource(String resourceName, String userName,
                                  String resourceTime, Integer categoryId,
                                  Integer pageNum, Integer pageSize) throws ParseException {
        PageHelper.startPage(pageNum,pageSize);
        Example example = new Example(Resource.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("resourceCid", categoryId);
        if (StringUtils.isNotBlank(resourceName) || StringUtils.isNotBlank(userName) ||
                StringUtils.isNotBlank(resourceTime)) {
            if (StringUtils.isNotBlank(resourceName)) {
                criteria.andLike("resourceName", "%" + resourceName + "%");
            }
            if (StringUtils.isNotBlank(userName)) {
                criteria.andLike("resourceUid", "%" + userName + "%");
            }
            if (StringUtils.isNotBlank(resourceTime)) {
                DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
                Date date = format1.parse(resourceTime.substring(1, resourceTime.length() - 1));
                criteria.andEqualTo("resourceTime", date);
            }
        }
        example.orderBy("resourceTime").desc();
        List<Resource> resourceList = resourceMapper.selectByExample(example);

        PageInfo pageInfo = new PageInfo<>(resourceList);
        pageInfo.setList(resourceList);

        return pageInfo;
    }



    /**
     * 删除资源
     *
     * @param resourceId
     * @return
     */
    @Transactional( propagation = Propagation.REQUIRED)
    @Override
    public JSONResult delResource(Integer resourceId) {
        Resource resource = new Resource();
        resource.setResourceId(resourceId);
        int i = resourceMapper.deleteByPrimaryKey(resource);
        if (i != 1) {
            return JSONResult.errorMsg("删除资源失败");
        }
        return JSONResult.ok("删除资源成功");
    }


}
